﻿using Chloe.SqlServer;
using SorketServer.HYModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SorketServer
{
    public partial class CarIn : Form
    {
        int type = 0;
        public CarIn(string carPlant,int _type)
        {
            InitializeComponent();
            this.txtPlant.Text = carPlant;
            this.type = _type;
            this.Load += CarIn_Load;
        }

        private void CarIn_Load(object sender, EventArgs e)
        {
            txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (type == 1)
            {
                CarRecord carRecord = CarHelper.SaveCarIn(txtPlant.Text.Trim());
                this.txtCompany.Text = carRecord.Company;
                this.txtReason.Text = carRecord.Reason;
                if (carRecord.InTime.HasValue)
                {
                    this.txtTime.Text = carRecord.InTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                }
              
                this.txtUserName.Text = carRecord.UserName;
                this.txtUserNum.Text = carRecord.PersonNum.ToString();
                this.txtUserNum.Enabled = this.txtCompany.Enabled = this.txtReason.Enabled =
                     this.txtTime.Enabled = this.txtUserName.Enabled = false;

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (type == 0)
                {
                    CarRecord carRecord = new CarRecord();
                    carRecord.CarPlant = this.txtPlant.Text.Trim();
                    carRecord.Company = this.txtCompany.Text.Trim();
                    carRecord.Id = Guid.NewGuid();
                    carRecord.InTime = DateTime.Parse(txtTime.Text.Trim());
                    carRecord.PersonNum = Convert.ToInt32(txtUserNum.Text.Trim());
                    carRecord.UserName = this.txtUserName.Text.Trim();
                    carRecord.Reason = this.txtReason.Text.Trim();
                    CarHelper.SaveCarIn(carRecord);
                }
                else
                {
                    CarRecord carRecord = new CarRecord();
                    carRecord.CarPlant = this.txtPlant.Text.Trim();
                    carRecord.Company = this.txtCompany.Text.Trim();
                    carRecord.Id = Guid.NewGuid();
                    carRecord.InTime = DateTime.Parse(txtTime.Text.Trim());
                    carRecord.PersonNum = Convert.ToInt32(txtUserNum.Text.Trim());
                    carRecord.UserName = this.txtUserName.Text.Trim();
                    carRecord.Reason = this.txtReason.Text.Trim();
                    CarHelper.UpdateCarIn(carRecord);

                }
                MessageBox.Show("保存成功");
                this.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
          

           
        }
    }
}
